import type { cartGoods, cartList, productList } from '@/types/cart'

export const _allOrHalfCheck = (tempList: any, data: any, tag: any) => {
  // 选中的数量
  let len = tempList.filter((item: any) => item.isCheck).length
  if (tag == 1) {
    // 这里是全部的数量  如果是最后一级 要排除掉 失效得商品
    let tempLen = tempList.filter((item: any) => item.putaway == 1 && item.stock > 0)
    // 判断半选
    data.indeterminate = len > 0 && len < tempLen.length
    // 判断全选
    data.isCheck = len === tempLen.length
  }

  if (tag == 2 || tag == 3) {
    // 判断半选
    data.indeterminate =
      (len > 0 && len < tempList.length) ||
      tempList.filter((item: any) => item.indeterminate).length > 0
    // 判断全选
    data.isCheck = len == tempList.length
  }
}

export const _setStatusToData = (cartList: cartList[]) => {
  let invalidProduct: (productList & { goodId: number })[] = []
  let productIds: number[] = []
  let goodsIds: number[] = []
  let isChooseCount = 0
  let totalAmount = 0

  cartList.map((goods) => {
    let cartGoods = goods.cartGoods
    cartGoods.map((product) => {
      let goodsList = product.goodsList
      goodsList.map((pro) => {
        let list = pro.productList
        list.map((item) => {
          if (item.putaway == 1 && item.stock > 0 && item.isChoose == 1) {
            item.isCheck = true
            productIds.push(item.cartId)
            goodsIds.push(pro.goodId)
            // 计算统计值
            totalAmount += item.amount * 100
            isChooseCount += item.qty
          } else {
            item.isCheck = false
          }

          if (item.putaway === 0 || item.stock < 1) {
            item.goodsImg = pro.goodsImg
            invalidProduct.push({ ...item, goodId: pro.goodId })
            item.isHide = true
          }
        })

        // 判断失效上商品
        let arr = list.filter((i) => i.isHide)
        if (list.length == arr.length) {
          pro.isHide = true
        }
      })

      let arr = goodsList.filter((i) => i.isHide)
      if (goodsList.length == arr.length) {
        product.isHide = true
      }
    })

    let arr = cartGoods.filter((i) => i.isHide)
    if (cartGoods.length == arr.length) {
      goods.isHide = true
    }
  })

  cartList.map((goods) => {
    goods.cartGoods.map((product) => {
      product.goodsList.map((pro) => {
        _allOrHalfCheck(pro.productList, pro, 1)
      })
    })
  })

  cartList.map((goods) => {
    goods.cartGoods.map((product) => {
      _allOrHalfCheck(product.goodsList, product, 2)
    })
  })

  cartList.map((goods) => {
    _allOrHalfCheck(goods.cartGoods, goods, 3)
  })

  return {
    cartList: cartList,
    invalidProduct: invalidProduct,
    cartIds: productIds.join(','),
    goodsIds: goodsIds,
    isChooseCount: isChooseCount,
    totalAmount: totalAmount / 100,
  }
}

export const filterData = (list: string, keyVal: string): boolean => {
  const dataList = JSON.parse(JSON.stringify(list))
  return dataList.filter((cart: cartList) => {
    let cartGoods = cart.cartGoods.filter((goods: cartGoods) => {
      let goodsList = goods.goodsList.filter((product) => {
        let productList = product.productList.filter((item) =>
          item.sourcesList.includes(parseInt(keyVal)),
        )
        product.productList = productList
        return productList.length > 0
      })
      goods.goodsList = goodsList
      return goodsList.length > 0
    })

    return cartGoods.length > 0
  })
}
